package com.wiz.weixin.controllor;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;

import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wiz.weixin.VO.ProductDiscount;
import com.wiz.weixin.common.Constant;
import com.wiz.weixin.common.GlobalConstant;
import com.wiz.weixin.common.TencentConstant;
import com.wiz.weixin.manager.HttpManager;
import com.wiz.weixin.manager.TencentManager;
import com.wiz.weixin.service.OilStationService;
import com.wiz.weixin.util.JSSDKUtil;
import com.wiz.weixin.util.PastUtil;
/**
 * 微信油站功能
 * @author xxy
 *
 */
@Controller
@RequestMapping("/oilstation")
public class OilStationControllor {
	private transient final Logger log=LoggerFactory.getLogger(getClass());
	@Resource
	private OilStationService oilStationService;
	@Resource
	private HttpManager httpManager;
	@Resource
	private TencentManager tencentManager;
	
	/**扫一扫*/
	@RequestMapping(value="/toEditRichScan")
	public String toEditRichScan(HttpServletRequest request,HttpServletResponse response,
			HttpSession session, Model model)throws ServletException, IOException {
		 // 将请求、响应的编码均设置为UTF-8（防止中文乱码）  
        request.setCharacterEncoding("UTF-8");  
        response.setCharacterEncoding("UTF-8"); 
        String code=request.getParameter("code");//签名串
        
        String appid =  TencentConstant.WX_APP_ID; //随机字符串
        String ticket = GlobalConstant.tencent_wx_ticket;
        try {
			String openid = tencentManager.getOpenId(code);
			Map<String, String>map = PastUtil.getParam(appid, ticket);
			map.put("code", code);
			System.out.println("code=============>"+code);
			map.put("openid", openid);
			model.addAttribute("map", map);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        

        
		return "/richScan/scan";
	}
	/**当前油价*/
	@RequestMapping(value="/listOilPrice")
	public String listOilPrice(HttpServletRequest request,HttpServletResponse response,
			HttpSession session, Model model) {
		
		String code = request.getParameter("code");
		try {
			String openid = tencentManager.getOpenId(code);//"ovNRGs8IVk3ctBk5YLcxE45XG50g" ;
			
//			getMember(session,openid);
	       
			
			System.out.println("当前油价========>"+code);
			System.out.println("listOilPrice========>"+openid);
			
			Map<String, List<ProductDiscount>> map = oilStationService.listOilPrice(openid);
			model.addAttribute("map", map);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}
		return "/oilstation/listOilPrice";
	}
	/**油站介绍
	 * @throws UnsupportedEncodingException */
	@RequestMapping(value="/litDescribe")
	public String litDescribe(HttpServletRequest request,HttpServletResponse response,
			HttpSession session, Model model) throws UnsupportedEncodingException {
		
		String code = request.getParameter("code");
		try {
			String openid = tencentManager.getOpenId(code);
			JSONArray jsonArray = oilStationService.getDescribe(openid);
			model.addAttribute("jsonList", jsonArray);
		} catch (JSONException e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}
		return "/oilstation/describe";
	}
	
	/**js sdk 验证*/
	@RequestMapping(value="/jsVerification")
	public String jsVerification(HttpServletRequest request,HttpServletResponse response,
			HttpSession session, Model model) throws ServletException, IOException{
		// 将请求、响应的编码均设置为UTF-8（防止中文乱码）  
		request.setCharacterEncoding("UTF-8");  
		response.setCharacterEncoding("UTF-8");
		String code=request.getParameter("code");//签名串
		System.out.println("code=============>"+code);
		
		String appid =  TencentConstant.WX_APP_ID; //随机字符串
		String ticket = GlobalConstant.tencent_wx_ticket;
		try {
			String openid = tencentManager.getOpenId(code);
			Map<String, String>map = PastUtil.getParam(appid, ticket);
//			JSONArray jsonArray = oilStationService.getDescribe(openid);
//			model.addAttribute("jsonList", jsonArray);
			map.put("code", code);
//			map.put("openid", openid);
			model.addAttribute("map", map);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return "/navigation/navigation";
	}
	
	/**得到附近油站*/
	@RequestMapping(value="/litAroundStation")
    public String litAroundStation(HttpServletRequest request,HttpServletResponse response,
    		HttpSession session, Model model) throws ServletException, IOException{
		 // 将请求、响应的编码均设置为UTF-8（防止中文乱码）  
        request.setCharacterEncoding("UTF-8");  
        response.setCharacterEncoding("UTF-8");
		 String code=request.getParameter("code");//签名串
	        
	        String appid =  TencentConstant.WX_APP_ID; //随机字符串
	        String ticket = GlobalConstant.tencent_wx_ticket;
	        try {
				String openid = tencentManager.getOpenId(code);
				Map<String, String>map = PastUtil.getParam(appid, ticket);
				JSONArray jsonArray = oilStationService.getDescribe(openid);
				model.addAttribute("jsonList", jsonArray);
				map.put("code", code);
//				map.put("openid", openid);
				model.addAttribute("map", map);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        
        return "/oilstation/navigation";
    }
	
	public void getMember(HttpSession session,String openId){
		 List<String>list = (List<String>) session.getAttribute("hqIds");
	        if(list == null){
	        	//查询会员信息
	    		Map<String,String> params = new HashMap<String,String>();
	    		params.put("openId", openId);
	    		// 调用接口创建菜单
	    		
	        	String response = httpManager.getHttpsResponseByPost(Constant.QUERY_MEMBER_URL, params, "utf-8");
	        	System.out.println("==========="+response);
	        
	        }
	}
}
